# coding: utf-8
from inspect import signature

from PySide6.QtWidgets import QApplication, QWidget
from qfluentwidgets import FluentIcon as FIF, NavigationItemPosition, FluentWindow

from common import SingleIcon, signalBus, APP_NAME, TaskStatus, cfg
from components import Alert
from threads import SqlResponse, DbThread
from .setting_interface import SettingInterface
from .task_interfacce import TaskInterface
from .tools_interface import ToolsInterface


class MainWindow(FluentWindow):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.__initWidgets()

        self.taskInterface = TaskInterface(self)
        self.toolsInterface = ToolsInterface(self)
        self.settingInterface = SettingInterface(self)

        self.__initSubInterface()
        self.__connectSignalToSlot()

    def __initWidgets(self):
        self.setMinimumSize(1000, 600)
        self.setWindowTitle(APP_NAME)
        self.setWindowIcon(SingleIcon.LOGO_PNG.icon())
        self.navigationInterface.setExpandWidth(200)
        self.navigationInterface.setAcrylicEnabled(True)
        self.setMicaEffectEnabled(cfg.get(cfg.micaEnabled))

        primary_size = QApplication.primaryScreen().availableSize()
        self.move((primary_size.width() - self.width()) // 2, (primary_size.height() - self.height()) // 2)

        self.dbThread = DbThread(self)
        self.dbThread.failed.connect(Alert.error)
        signalBus.dbResultSig.connect(self.onDbResult)

    def __initSubInterface(self):
        self.addSubInterface(self.taskInterface, FIF.TILES, "全部任务", isTransparent=True)

        self.navigationInterface.addSeparator()
        self.navigationInterface.addItem(
            'downloading',
            FIF.CLOUD_DOWNLOAD,
            "下载中",
            onClick=lambda: self.switchTo(self.taskInterface, TaskStatus.DOING),
            tooltip="下载中"
        )
        self.navigationInterface.addItem(
            'finished',
            FIF.COMPLETED,
            "已完成",
            onClick=lambda: self.switchTo(self.taskInterface, TaskStatus.DONE),
            tooltip="已完成"
        )
        self.navigationInterface.addItem(
            'stopped',
            FIF.PAUSE,
            "已停止",
            onClick=lambda: self.switchTo(self.taskInterface, TaskStatus.PAUSE),
            tooltip="已停止"
        )
        self.navigationInterface.addItem(
            'failed',
            FIF.INFO,
            "失败",
            onClick=lambda: self.switchTo(self.taskInterface, TaskStatus.ERROR),
            tooltip="失败"
        )
        self.navigationInterface.addSeparator()

        btm = NavigationItemPosition.BOTTOM
        self.navigationInterface.addSeparator(btm)
        self.addSubInterface(self.toolsInterface, FIF.APPLICATION, "小工具", btm, isTransparent=True)
        self.addSubInterface(self.settingInterface, FIF.SETTING, "设置", btm, isTransparent=True)

    def __connectSignalToSlot(self):
        signalBus.micaEnableChanged.connect(self.setMicaEffectEnabled)

    def taskStatusShow(self, status: TaskStatus):
        self.switchTo(self.taskInterface)
        self.taskInterface.showTaskByStatus(status)

    # @property
    # def backgroundColor(self):
    #     color = QLinearGradient(0, 0, self.width(), self.height())
    #     if isDarkTheme():
    #         color.setColorAt(0, QColor(148, 0, 211))
    #         color.setColorAt(1, QColor(75, 0, 130))
    #     else:
    #         color.setColorAt(0, QColor(206, 159, 252))
    #         color.setColorAt(1, QColor(206, 159, 252))
    #     return color

    def onDbResult(self, response: SqlResponse):
        slot = response.slot
        result = response.result
        if not slot:
            return
        sig = signature(slot)
        if len(sig.parameters) != 0:
            slot(result)
        else:
            slot()

    def switchTo(self, interface: QWidget, status=None):
        super().switchTo(interface)
        if interface == self.taskInterface:
            self.taskInterface.showTaskByStatus(status)
